<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Models\ExpressModel;
use App\Http\Models\ExpressorderModel;
use Validator;
use Session;

class ExpressController extends Controller
{

    public function index()
    {
        //$data['data'] = ExpressModel::paginate(10)->toArray();

        //$data=$data['data'];

        $data['data'] = ExpressModel::paginate(10);
        $data['type']=3 ;
        return view('express.index',compact('data'));
    }

    public function edit(Request $request,$id)
    {
        if ($request->isMethod('get')){
            $data = ExpressModel::where(['id' => $id])->first()->toArray();
            return view('express.edit', compact('data'));
        }else{
            $data = $request->except(['_token','uploadfile']);
            $validator = Validator::make($data, [
                'name'     => 'required|string',
            ]);
            if ($validator->fails()) {
                return redirect('/admin/express/add')
                    ->withErrors($validator)
                    ->withInput($data);
            }
            ExpressModel::where(['id'=>$id])->update($data);
            return redirect('/admin/express/index');
        }
    }
    public function add(Request $request){
        if ($request->isMethod('get')){
            return view('express.add');
        }else{
            $data = $request->except(['_token','uploadfile']);
            $validator = Validator::make($data, [
                'name'     => 'required|string',
            ]);
            if ($validator->fails()) {
                return redirect('/admin/express/add')
                    ->withErrors($validator)
                    ->withInput($data);
            }
            ExpressModel::create($data);
            return redirect('/admin/express/index');
        }
    }

    public function show_info(){
        $data=ExpressModel::all();
        return  $data;
    }

    public function del(Request $request)
    {
        $id = $request->input('id');
        $res = ExpressModel::where(['id'=>$id])->first();
        if(!empty($res)) {
            ExpressModel::where(['id'=>$id])->delete();
            return response()->json(['status' => 200, 'msg' =>'删除成功']);
        }
        return response()->json(['status' => 500, 'msg' =>'删除失败']);

    }

    public function expresslist()
    {
        $data['data'] = ExpressorderModel::paginate(10);
        $data['type']=3 ;
        return view('express.index',compact('data'));

    }


}
